<?php
namespace App\Repositories;

/**
 * 基础的数据仓库类
 *
 * Class BaseRepository
 * @package App\Repositories
 * @author gpc
 */
trait BaseRepository
{
    // 静态变量
    protected static $db;

    /**
     * 依赖注入
     *
     * BaseRepository constructor.
     * @author zhouyao
     */
    public function __construct()
    {
        self::$db = app('db');
    }

    /**
     * 添加单条数据
     *
     * @param $param
     * @return bool
     * @author zhouyao
     */
    public function addData($param)
    {
        if (empty($param)) {
            return false;
        }

        return self::$db->table(static::$table)->insertGetId($param);
    }

    /**
     * 更新数据
     *
     * @param $where
     * @param $data
     * @return bool
     * @author  zhouyao
     */
    public function updateData($where, $data)
    {
        if (empty($where) || empty($data)) {
            return false;
        }

        return self::$db->table(static::$table)->where($where)->update($data);
    }

    /**
     * 查询一条记录
     *
     * @param $where
     * @param $field
     * @return bool
     * @author zhouyao
     */
    public function getOneData($where, $field = [])
    {
        if (empty($where)) {
            return false;
        }
        if ($field) {
            return self::$db->table(static::$table)->where($where)->select($field)->first();

        }
        return self::$db->table(static::$table)->where($where)->first();
    }

    /**
     * 获取所有数据
     *
     * @param array $where
     * @param array $select
     * @param string $field
     * @param string $desc
     * @return mixed
     * @author zhouyao
     */
    public function getAllData($where = [], $select = [], $field = 'id', $desc = 'asc')
    {
        if (empty($where)) {
            if (empty($select)) {
                return self::$db->table(static::$table)->orderBy($field, $desc)->get();
            }
            return self::$db->table(static::$table)->select($select)->orderBy($field, $desc)->get();
        }

        if (empty($select)) {
            return self::$db->table(static::$table)->where($where)->orderBy($field, $desc)->get();
        }

        return self::$db->table(static::$table)
            ->where($where)
            ->select($select)
            ->orderBy($field, $desc)
            ->get();
    }

    /**
     * 获取总条数
     *
     * @param array $where
     * @return mixed
     * @author zhouyao
     */
    public function getCount($where = [])
    {
        if (empty($where)) {
            return self::$db->table(static::$table)->count();
        }

        return self::$db->table(static::$table)->where($where)->count();
    }

    /**
     * 获取分页数据
     *
     * @param $nowPage
     * @param $offset
     * @param string $where
     * @param string $field
     * @param string $asc
     * @return bool
     * @author zhouyao
     */
    public function getPageData($nowPage, $offset, $where = '', $field = '', $asc='desc')
    {
        if (empty($nowPage) || empty($offset)) {
            return false;
        }

        $database = self::$db->table(static::$table)->forPage($nowPage, $offset);
        if (!empty($where)) {
            $database = $database->where($where);
        }

        if (!empty($field)) {
            $database = $database->orderBy($field, $asc);
        }

        return $database->get();
    }

    /**
     * 不返回ID的添加数据
     *
     * @param $param
     * @return bool
     * @author zhouyao
     */
    public function insertData($param)
    {
        if (empty($param)) {
            return false;
        }

        return self::$db->table(static::$table)->insert($param);
    }

    /**
     * 删除(带where条件删除)
     *
     * @param $where
     * @return bool
     * @author zhouyao
     */
    public function delete($where)
    {
        if (empty($where)) {
            return false;
        }

        return self::$db->table(static::$table)->where($where)->delete();
    }

    /**
     * 删除(带whereIn条件删除)
     *
     * @param $where
     * @param string $field
     * @return bool
     * @author zhouyao
     */
    public function deleteWhereIn($where, $field = 'id')
    {
        if (empty($where)) {
            return false;
        }

        return self::$db->table(static::$table)->whereIn($field, $where)->delete();
    }

    /**
     * 删除(带id删除，条件只能传id)
     *
     * @param $where
     * @return bool
     * @author zhouyao
     */
    public function deleteId($where)
    {
        if (empty($where)) {
            return false;
        }

        return self::$db->table(static::$table)->delete($where);
    }

    /**
     * 获取whereIn数据
     *
     * @param $where
     * @param $fieldName
     * @param $data
     * @param array $select
     * @return bool
     * @author zhouyao
     */
    public function getWhereInData($where, $fieldName, $data, $select = [])
    {
        if (empty($where)) {
            return false;
        }

        $database = self::$db->table(static::$table)->where($where);

        if (!empty($fieldName) && !empty($data)) {
            $database = $database->whereIn($fieldName, $data);
        }
        if (!empty($select)) {
            $database = $database->select($select);
        }

        return $database->get();
    }

    /**
     * 获取whereIn总数目数据
     *
     * @param $where
     * @param $fieldName
     * @param $data
     * @param array $select
     * @return bool
     * @author zhouyao
     */
    public function getWhereInCount($where, $fieldName, $data, $select = [])
    {
        if (empty($where) || empty($fieldName)) {
            return false;
        }

        $database = self::$db->table(static::$table)->where($where);

        if (!empty($data)) {
            $database = $database->whereIn($fieldName, $data);
        }
        if (!empty($select)) {
            $database = $database->select($select);
        }
        return $database->count();
    }
}